package com.zx.idc.backend.gui.base.oper.service;

import com.zx.idc.backend.gui.api.commom.entity.UploadModel;
import com.zx.idc.backend.gui.api.commom.template.NIdcFtpTemplate;
import com.zx.idc.backend.gui.api.management.basicdata.BaseIdcBasicDataService;
import com.zx.idc.backend.gui.api.management.basicdata.report.IdcBasicDataReportService;
import com.zx.idc.backend.gui.common.base.BaseReportService;
import com.zx.idc.common.attributes.FTPReportDataTypeEnum;
import com.zx.idc.common.attributes.NetCommunicationFileTypeEnum;
import com.zx.idc.common.attributes.SchemaFileNameEnum;
import com.zx.idc.ds.base.house.entity.BaseHouseOfficer;
import com.zx.idc.ds.base.house.service.IBaseHouseOfficerService;
import com.zx.idc.ds.base.oper.entity.BaseOperCompany;
import com.zx.idc.ds.base.oper.service.IBaseOperCompanyService;
import com.zx.idc.ds.common.constant.DataFeatureConstant;
import com.zx.idc.ds.common.constant.ReportStatusConstant;
import com.zx.idc.xml.entity.basedata.BasicInfo;
import com.zx.idc.xml.entity.basedata.basic.IdcOfficer;
import io.vavr.control.Option;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;
import java.util.function.Consumer;

/**
 * @author rxliuli
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BaseOperCompanyReportService implements BaseReportService<String> {
    @Autowired
    private IBaseOperCompanyService service;
    @Autowired
    private NIdcFtpTemplate nIdcFtpTemplate;
    @Autowired
    private IdcBasicDataReportService idcBasicDataReportService;
    @Autowired
    private BaseIdcBasicDataService baseIdcBasicDataService;
    @Autowired
    protected IBaseOperCompanyService baseOperCompanyService;
    @Autowired
    protected IBaseHouseOfficerService baseHouseOfficerService;


    @Override
    public boolean insertReport(String idcId) {
        //activeReporting(idcBasicDataReportService.operCompanyAddReport(idcId));
        insertOrAddActiveReporting(idcBasicDataReportService.operCompanyAddReport(idcId));
        return true;
    }

    @Override
    public boolean updateReport(String idcId) {
        //activeReporting(idcBasicDataReportService.operCompanyUpdateReport(idcId));
        insertOrAddActiveReporting(idcBasicDataReportService.operCompanyUpdateReport(idcId));
        return true;
    }

    @Override
    public boolean deleteReport(String idcId) {
        activeReporting(idcBasicDataReportService.operCompanyDeleteReport(idcId));
        return true;
    }

    //@Transactional
    private void activeReporting(BasicInfo basicInfo) {
        baseIdcBasicDataService.update(basicInfo, ReportStatusConstant.REPORTING);
        nIdcFtpTemplate.uploadXMLFile(
            new UploadModel<>(
                basicInfo,
                SchemaFileNameEnum.BASE_DATA_REPORT,
                FTPReportDataTypeEnum.FTP_BASE_DATA,
                NetCommunicationFileTypeEnum.BASE_DATA
            )
        );
    }

    /**
     * 后期修改20200506
     * 新增上报,为了不影响之前的操作(处理上报结果新增与修改的上报结果),把逻辑抽离
     *
     * @param basicInfo
     */
    @Transactional
    public void insertOrAddActiveReporting(BasicInfo basicInfo) {
        //baseIdcBasicDataService.update(basicInfo, ReportStatusConstant.REPORTING);
        final Consumer<String> companyProcessor = en -> Option.of(en)
            .map(idcId -> new BaseOperCompany()
                .setIdcId(idcId)
                .setReportStatus(ReportStatusConstant.REPORTING)
            )
            .filter(Objects::nonNull)
            .peek(baseOperCompanyService::updateIgnoreDelFlag);
        Consumer<Option<String>> officerProcessor = s ->
            s.filter(NumberUtils::isCreatable)
                .map(id -> new BaseHouseOfficer()
                    .setId(Long.parseLong(id))
                    .setReportStatus(ReportStatusConstant.REPORTING))
                .peek(baseHouseOfficerService::updateIgnoreDelFlag);

        final Consumer<IdcOfficer> idcOfficerProcessor = en -> Option.of(en)
            .map(IdcOfficer::getId)
            .map(Option::of)
            .peek(officerProcessor);

        // region 更新新增信息
        Option.of(basicInfo)
            .map(BasicInfo::getNewInfo)
            .filter(Objects::nonNull)
            .peek(newInfo -> {
                BaseOperCompany baseOperCompany = baseOperCompanyService.selectOne(new BaseOperCompany().setIdcId(newInfo.getIdcId()));
                if (baseOperCompany.getReportStatus() == ReportStatusConstant.NOT_REPORTED && baseOperCompany.getDataFeature() == DataFeatureConstant.NEW) {
                    companyProcessor.accept(newInfo.getIdcId());
                }
            }).peek(newInfo -> idcOfficerProcessor.accept(newInfo.getIdcOfficer()));

        //endregion

        // region 更新修改信息
        Option.of(basicInfo)
            .map(BasicInfo::getUpdateInfo)
            .filter(Objects::nonNull)
            .peek(updateInfo -> {
                BaseOperCompany baseOperCompany = baseOperCompanyService.selectOne(new BaseOperCompany().setIdcId(updateInfo.getIdcId()));
                if (baseOperCompany.getReportStatus() == ReportStatusConstant.NOT_REPORTED && baseOperCompany.getDataFeature() == DataFeatureConstant.UPDATE) {
                    companyProcessor.accept(updateInfo.getIdcId());
                }
            })
            .peek(updateInfo -> idcOfficerProcessor.accept(updateInfo.getIdcOfficer()));
        // endregion

        nIdcFtpTemplate.uploadXMLFile(
            new UploadModel<>(
                basicInfo,
                SchemaFileNameEnum.BASE_DATA_REPORT,
                FTPReportDataTypeEnum.FTP_BASE_DATA,
                NetCommunicationFileTypeEnum.BASE_DATA
            )
        );
    }


}
